
/**
 * 文件下载
 *
 * @param res 文件数据
 * @param msg 文件名称(当header无数据时使用)
 * @param type  文件类型
 * @param isTime  是否显示时间
 */
export const downloadFile = (fileArrayBuffer, filename) => {
  let data = new Blob([fileArrayBuffer], {
    type: 'application/vnd.ms-excel,charset=utf-8',
  });
  if (typeof window.chrome !== 'undefined') {
    // Chrome
    var link = document.createElement('a');
    link.href = window.URL.createObjectURL(data);
    link.download = filename;
    link.click();
  } else if (typeof window.navigator.msSaveBlob !== 'undefined') {
    // IE
    var blob = new Blob([data], { type: 'application/force-download' });
    window.navigator.msSaveBlob(blob, filename);
  } else {
    // Firefox
    var file = new File([data], filename, {
      type: 'application/force-download',
    });
    window.open(URL.createObjectURL(file));
  }
};

export const download = (url, filename) => {
  let downloadElement = document.createElement('a');
  let href = url; //创建下载的链接
  let reg = /^["](.*)["]$/g;
  downloadElement.style.display = 'none';
  downloadElement.href = filename ? href : '#';
  downloadElement.download = filename
    ? decodeURI(filename.replace(reg, '$1'))
    : url; //下载后文件名
  document.body.appendChild(downloadElement);
  downloadElement.click(); //点击下载
  document.body.removeChild(downloadElement); //下载完成移除元素
  window.URL.revokeObjectURL(href); //释放掉blob对象
};
